package middleware

import (
	"gitee.com/keepalived_0807/Vblog/apps/token"
	"gitee.com/keepalived_0807/Vblog/ioc"
	"gitee.com/keepalived_0807/Vblog/response"
	"github.com/gin-gonic/gin"
)

func Auth(c *gin.Context) {
	atk, err := c.Cookie(token.COOKIE_TOKEN_KEY)
	if err != nil {
		response.Failed(token.ErrUnAuthed.WithMessage(err.Error()), c)
		c.Abort()
	}

	tk, err := ioc.Controller.Get(token.AppName).(token.Service).ValidateToken(c.Request.Context(), token.NewValidateTokenRequest(atk))
	if err != nil {
		response.Failed(token.ErrAuthFailed.WithMessage(err.Error()), c)
		c.Abort()
	} else {
		c.Set("token.GIN_TOKEN_KEY_NAME", tk)
		c.Next()
	}
}

// func (h *BlogApiHandler) auth(c *gin.Context) bool {
// 	tk, _ := c.Cookie(token.COOKIE_TOKEN_KEY)
// 	ioc.Controller.Get(token.AppName).(token.Service).ValidateToken(c.Request.Context(), token.NewValidateTokenRequest(tk))
// 	return true
// }
